using System.Windows.Controls;
using System.Windows.Documents;
using com.bodurov.SilverlightControls.XmlCodeEditor.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace com.bodurov.SilverlightControls.XmlCodeEditor.Tests
{
    [TestClass]//[Ignore]
    public class CanPerformDocumentsOperations : BaseTokenFixture
    {
        //<% Master %>
        //A
        private const string _xaml_sample_1 = "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                "<Paragraph>" +
                    "<Run Text=\"&lt;%\" />" +
                    "<Run Text=\" \" />" +
                    "<Run Text=\"Master\" />" +
                    "<Run Text=\" \" />" +
                    "<Run Text=\"%&gt;\" />" +
                "</Paragraph>" +
                "<Paragraph>" +
                    "<Run Text=\"A\" />" +
                "</Paragraph>" +
           "</Section>";

        //<% Master %>
        //
        //<Abc/>
        private const string _xaml_sample_2 = "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                "<Paragraph>" +
                    "<Run Text=\"&lt;%\" />" +
                    "<Run Text=\" \" />" +
                    "<Run Text=\"Master\" />" +
                    "<Run Text=\" \" />" +
                    "<Run Text=\"%&gt;\" />" +
                "</Paragraph>" +
                "<Paragraph/>" +
                "<Paragraph>" +
                    "<Run Text=\"&lt;\" />" +
                    "<Run Text=\"Abc\" />" +
                    "<Run Text=\"/&gt;\" />" +
                "</Paragraph>" +
           "</Section>";


        /*[TestMethod]//[Ignore]
        public void Test1()
        {
            var xaml = "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"+
                            "<Paragraph>"+
                                "<Run Text=\"&lt;%\" />"+
                                "<Run Text=\" \" />"+
                                "<Run Text=\"Master\" />"+
                                "<Run Text=\" \" />" +
                                "<Run Text=\"%&gt;\" />"+
                            "</Paragraph>"+
                            "<Paragraph>" +
                                "<Run Text=\"A\" />" +
                            "</Paragraph>" +
                       "</Section>";


            var rtb = new RichTextBox {Xaml = xaml};
            
            var pointer = rtb.ContentStart;

            Dbg.Trace(pointer.GetDescription());

            TextPointer lastPointer;
            while (true)
            {
                lastPointer = pointer;
                pointer = pointer.GetPositionAtOffset(1, LogicalDirection.Forward);

                Dbg.Trace(pointer.GetDescription());

                if(pointer == null)
                {
                    break;
                }
                rtb.Selection.Select(lastPointer, pointer);

                //Dbg.Trace(rtb.Selection.Xaml + "");
            }
            
        }
        */

        [TestMethod]//[Ignore]
        public void TextPointer_Can_GetCharacterOffsetFrom_Forward()
        {
            var rtb = new RichTextBox { Xaml = _xaml_sample_1 };

            var start = rtb.ContentStart;
            var end = rtb.ContentEnd;

            var num = start.GetCharacterOffsetFrom(end);

            AssertAreEqual(14, num);
        }

        [TestMethod]//[Ignore]
        public void TextPointer_Can_GetCharacterOffsetFrom_Backward()
        {
            var rtb = new RichTextBox { Xaml = _xaml_sample_1 };

            var start = rtb.ContentStart;
            var end = rtb.ContentEnd;

            var num = end.GetCharacterOffsetFrom(start);

            AssertAreEqual(-14, num);
        }

        [TestMethod]//[Ignore]
        public void TextPointer_Can_GetPointerAtCharacterOffset_Forward()
        {
            var rtb = new RichTextBox { Xaml = _xaml_sample_1 };

            var start = rtb.ContentStart;

            var pointer = start.GetPointerAtCharacterOffset(13);

            var pointer2 = pointer.GetPointerAtCharacterOffset(1);

            rtb.Selection.Select(pointer, pointer2);

            AssertAreEqual("A", rtb.Selection.Text);
        }

        [TestMethod]//[Ignore]
        public void TextPointer_Can_GetPointerAtCharacterOffset_Backward()
        {
            var rtb = new RichTextBox { Xaml = _xaml_sample_1 };

            var end = rtb.ContentEnd;

            var pointer = end.GetPointerAtCharacterOffset(-6);

            var pointer2 = pointer.GetPointerAtCharacterOffset(1);

            rtb.Selection.Select(pointer, pointer2);

            AssertAreEqual("r", rtb.Selection.Text);
        }

        [TestMethod]//[Ignore]
        public void TextPointer_Can_GetSubstring_Forward()
        {
            var rtb = new RichTextBox { Xaml = _xaml_sample_1 };

            var start = rtb.ContentStart;

            var pointer = start.GetPointerAtCharacterOffset(1);

            var pointer2 = start.GetPointerAtCharacterOffset(5);

            var substring = pointer.GetSubstring(pointer2);

            //Dbg.Trace("|"+substring+"|");

            AssertAreEqual("% Ma", substring);
        }

        [TestMethod]//[Ignore]
        public void TextPointer_Can_GetSubstring_Backward()
        {
            var rtb = new RichTextBox { Xaml = _xaml_sample_1 };

            var start = rtb.ContentStart;

            var pointer = start.GetPointerAtCharacterOffset(1);

            var pointer2 = rtb.ContentEnd;

            var substring = pointer2.GetSubstring(pointer);
            //var substring = pointer.GetSubstring(pointer2);

            //Dbg.Trace("|"+substring+"|");

            AssertAreEqual("% Master %>\nA", substring);
        }

        [TestMethod]//[Ignore]
        public void TextPointer_Can_GetSubstring_WithEmptyLine_Forward()
        {
            var rtb = new RichTextBox { Xaml = _xaml_sample_2 };

            var pointer = rtb.ContentStart;

            var pointer2 = rtb.ContentEnd;

            var substring = pointer.GetSubstring(pointer2);

            //Dbg.Trace("|"+substring+"|");

            AssertAreEqual("<% Master %>\n\n<Abc/>", substring);
        }
    }
}